/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

/* eslint-disable react/jsx-no-bind */
/**
 * 已禁用驿站列表
 */
import React, { Component } from 'react';
import { connect } from 'dva';
import { debounce } from 'lodash';
import AreaList from '@/components/AreaList';
import InfoCheck from '@/components/InfoCheck';
import Search from './Search';
import Pagination from './Pagination';

@connect(({ loading, area, user, setting }) => ({
  area,
  user,
  loading: loading.effects['area/subList'],
  ...setting,
}))
export default class Forbidden extends Component {
  constructor(props) {
    super(props);
    this.getAreaList = this.getAreaList.bind(this);
  }

  state = {
    current: 1,
    count: 1,
    pageSize: 10,
  };

  // 分页
  onChange = debounce(
    page => {
      this.getAreaList(page);
      this.setState({
        current: page,
      });
    },
    500,
    {
      leading: true,
      trailing: false,
    },
  );

  getAreaList(page) {
    const {
      dispatch,
      area,
      user: {
        currentUser: { user_info },
      },
    } = this.props;
    const {
      forbidden: { branch_id, keyword },
    } = area;
    dispatch({
      type: 'area/zyGetSubPostStations',
      payload: {
        page,
        status: 2,
        keyword: keyword || '',
        branch_id: branch_id || user_info.branchId,
        pageSize: 20,
      },
    });
  }

  handleInfoReady(data) {
    const {
      options: { key },
    } = this.props;
    if (!data.name && key !== 'post') return;
    this.getAreaList(1);
  }

  render() {
    const { loading, area } = this.props;
    const { current, count, pageSize } = this.state;
    const { forbidden } = area;
    const { paginations } = forbidden;
    const currentPage = paginations ? paginations.page : current;
    const totalNum = paginations ? paginations.count : count;
    const pageSizeNum = paginations ? paginations.pageSize : pageSize;
    return (
      <div>
        <InfoCheck onReady={this.handleInfoReady.bind(this)} />
        <Search searchType="5" current={currentPage} />
        <AreaList
          loading={loading}
          data={forbidden}
          post="1"
          zyForbidden
          getList={() => this.getAreaList(current)}
        />
        <Pagination
          current={currentPage * 1}
          count={totalNum}
          onChange={this.onChange}
          pageSize={pageSizeNum}
        />
      </div>
    );
  }
}
